/*
 * JSFConsole - Lightweight JSF Application Monitor
 * Copyright (C) 2009  Grzegorz Bugaj
 * http://www.gregbugaj.com
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/
package com.gregbugaj.reflector.producers;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

import org.w3c.dom.Element;
import org.w3c.dom.Node;

import com.gregbugaj.reflector.visitors.JSFDumpContext;

public class MethodNodeProducer extends BaseNodeProducer{

	private String signature;
	private String name;
	private String type;

	public MethodNodeProducer(JSFDumpContext context) {
		super(context);
	}

	public BaseNodeProducer set(Object value, Field f) {

		return this;
	}

	public MethodNodeProducer set(Object value, Method m) {
		objRefId = getRefId(value);
		String name="";
		String sType="";
		
		if(m!=null){
			 name=m.getName();
			 sType=m.getReturnType().getCanonicalName();
			 this.signature=m.toGenericString();
		}
		int index=signature.lastIndexOf('.')+1;
		signature=signature.split(" ")[0] +" "+ signature.substring(index);
		this.name=name;
		this.type=sType;
		return this;
	}

	/**
	 * //c("<field type=\""+simpleName+"\" name=\""+name+"\" value=\""+o+"\" />" , depth);
	 */
	@Override
	public Node getNode() {
		Element elem = context.getDocument().createElement("method");
		elem.setAttribute("name", name);
		elem.setAttribute("type", type);
		elem.setAttribute("signature", signature);

		elem.setAttribute("objRefId", objRefId);
		
		elem.setAttribute("opath", objRefId);
		return elem;
	}



}